<?php

/**
 * 后台系统登入页
 * Date: 2015/9/8
 * Time: 14:54
 */
namespace AdminV3\Controller;
use Org\Util\Rbac;
use Think\Controller;

class IndexController extends Controller
{
    public function index(){

        $this->display();
    }
    public function verify(){
        ob_clean();

        $config =    array(

            'length'      =>    4,     // 验证码位数
            /*'useNoise'    =>    false,*/ // 关闭验证码杂点
            'useCurve'    =>    false, //关闭曲线
        );

        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    public function logindo(){

        $username = I('post.username');
        $psw = md5(I('post.password'));
        $verify = I('post.verify');

        if(empty($username)){
            $this->error('用户名必须填写',U('Index/index'),2);
        }

       if(empty($psw)){
            $this->error('密码必须填写',U('Index/index'),2);
        }

        if(empty( $verify)){
            $this->error('验证码必须填写',U('Index/index'),2);
        }
        //echo $_POST['verify'];die();
        //验证码检测
        if(!check_verify($_POST['verify'])){
            $this->error('验证码错误',U('Index/index'),2);
        }

        $M = M('user');
        $where = "username='".$username."' and password='".$psw."' and status = 1";
        $list = $M->where($where)->select();
        //dump($list);
        //echo $M->getLastSql();die();
        if(empty($list)){
            $this->error('用户名或密码错误 或则 账号异常!',U('Index/index'),1);
        }else{
            //session 保存数据.
            session('id',$list[0]['id']);
            session('username',$list[0]['username']);
            session('name',$list[0]['name']);
            session('page',$list[0]['page']);
            $roleId = M('roleUser')->where('user_id ='.$list[0]['id'])->field('role_id')->select();
            $roleData = M('role')->where('id ='.$roleId[0]['role_id'])->select();
            session('roleid',$roleData[0]['id']);
            session('rolename',$roleData[0]['name']);

            //保存时间用于判断session周期.
            session('time',time());
            //保存session 会话标识，用来后面判断用户已经登录的状态
            session(C('USER_AUTH_KEY'), $list[0]['id']);

            //超级管理员身份认证
            if($list[0]['username'] === 'admin'){
                //该用户不受认证控制
                session(C('ADMIN_AUTH_KEY'),true);
            }

            //缓存访问权限
            Rbac::saveAccessList();

            $this->redirect('/AdminV3/Console/index');

        }

    }


    public function logout(){

        session('[destroy]'); // 销毁session
        $this->display('index');
    }
}